<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {

	//系统首页
    public function index(){
    	$category = D('Category')->info('freshberry');
    	$freshberrylist = D('Document')->page(1, 6)->lists($category['id']);
    	$category = D('Category')->info('interisnet');
    	$interisnetlist = D('Document')->page(1, 6)->lists($category['id']);

    	$this->assign('freshberrylist',$freshberrylist);//列表
        $this->assign('interisnetlist',$interisnetlist);//列表
        $this->display();
    }

    public function getmore($p){
    	$category = $this->category();
    	$list = D('Document')->page($p, $category['list_row'])->lists();
    	foreach ($list as $key => &$value) {
    		$value["img_url"]= get_cover($value['cover_id'],$field = path);
    		$value["detail_url"]= U('Article/detail?id='.$value['id']);
    		$value["author_name"]= get_nickname($value['uid']);
    		$value["create_time_f"]= date("Y-m-d H:i", $value['create_time']);
    		$value["category_url"]= U('Article/lists?category='.$value['category_id']);
    		$value["category_title"]= get_category_title($value['category_id']);
    		$value["category_color"]= get_category_gradient($value['category_id']);
    		$value["category_rgba"]= html2rgb($value['category_id']);
    	}
		echo json_encode($list);
    }

    /* 文档分类检测 */
	private function category($id='default_blog'){
		/* 标识正确性检测 */
		if(empty($id)){
			$this->error('没有指定文档分类！');
		}

		/* 获取分类信息 */
		$category = D('Category')->info($id);
		
		if($category && 1 == $category['status']){
			switch ($category['display']) {
				case 0:
					$this->error('该分类禁止显示！');
					break;
				//TODO: 更多分类显示状态判断
				default:
					return $category;
			}
		} else {
			$this->error('分类不存在或被禁用！');
		}
	}
}